import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MallSeckillConfigApi } from '#/api/mall/promotion/seckill/seckillConfig';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { $t } from '#/locales';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: $t('views.mall.promotion.seckill.config.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.mall.promotion.seckill.config.namePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'startTime',
      label: $t('views.mall.promotion.seckill.config.startTime'),
      component: 'TimePicker',
      componentProps: {
        format: 'HH:mm',
        valueFormat: 'HH:mm',
        placeholder: $t('views.mall.promotion.seckill.config.startTimePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'endTime',
      label: $t('views.mall.promotion.seckill.config.endTime'),
      component: 'TimePicker',
      componentProps: {
        format: 'HH:mm',
        valueFormat: 'HH:mm',
        placeholder: $t('views.mall.promotion.seckill.config.endTimePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'sliderPicUrls',
      label: $t('views.mall.promotion.seckill.config.sliderPicUrls'),
      component: 'ImageUpload',
      componentProps: {
        multiple: true,
        maxNumber: 5,
      },
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: 'required',
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: $t('views.mall.promotion.seckill.config.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.mall.promotion.seckill.config.namePlaceholder'),
      },
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'Select',
      componentProps: {
        placeholder: $t('common.statusPlaceholder'),
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
      },
    },
  ];
}

/** 表格列配置 */
export function useGridColumns(
  onStatusChange?: (
    newStatus: number,
    row: MallSeckillConfigApi.SeckillConfig,
  ) => PromiseLike<boolean | undefined>,
): VxeTableGridOptions['columns'] {
  return [
    {
      title: $t('views.mall.promotion.seckill.config.name'),
      field: 'name',
      minWidth: 200,
    },
    {
      title: $t('views.mall.promotion.seckill.config.startTime'),
      field: 'startTime',
      minWidth: 120,
    },
    {
      title: $t('views.mall.promotion.seckill.config.endTime'),
      field: 'endTime',
      minWidth: 120,
    },
    {
      title: $t('views.mall.promotion.seckill.config.sliderPicUrls'),
      field: 'sliderPicUrls',
      minWidth: 100,
      cellRender: {
        name: 'CellImages',
      },
    },
    {
      title: $t('common.status'),
      field: 'status',
      minWidth: 100,
      cellRender: {
        attrs: { beforeChange: onStatusChange },
        name: 'CellSwitch',
        props: {
          checkedValue: 1,
          checkedChildren: $t('common.enabled'),
          unCheckedValue: 0,
          unCheckedChildren: $t('common.disabled'),
        },
      },
    },
    {
      title: $t('common.createTime'),
      field: 'createTime',
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      title: $t('common.action'),
      width: 180,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
